package com.zhu.exercise.array;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Addone {
    public int[] plusOne(int[] digits) {
        boolean carry =false;
        List<Integer> result = new ArrayList<>();
        for (int i = digits.length-1; i >=0 ; i--) {
            int num = digits[i];

            if(i == digits.length-1){
                num+=1;
                if(num>=10){
                    carry=true;
                }
            }else if(carry){
                num+=1;
                if(num<10){
                    carry=false;
                }
            }
            num=num%10;
            result.add(num);
        }
        if(carry){
            result.add(1);
        }
        Collections.reverse(result);
        return result.stream().mapToInt(Integer::valueOf).toArray();
    }

    @Test
    public void run(){
//        int[] array1 = {1,2,3};
        int[] array1 = {9,9,9};
        int[] result = plusOne(array1);
        Arrays.stream(result).forEach(value -> System.out.println(value));
    }
}
